//
//  LDCMyCenterViewController.m
//  LaiKeDaoClient
//
//  Created by zouzhisheng on 15/9/28.
//  Copyright © 2015年 LKD. All rights reserved.
//

#import "LDCMyCenterViewController.h"
#import "CustomCenterHeadTableViewCell.h"
#import "CustomCenterFootTableViewCell.h"
#import "KDSettingViewController.h"
#import "KDInforViewController.h"
#import "KDPatientJudgeViewController.h"
#import "UIImageView+WebCache.h"
@implementation MyCenterNavigationController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor clearColor];
    LDCMyCenterViewController *addPatientVC=[[LDCMyCenterViewController alloc] init];
    self.viewControllers = @[addPatientVC];
}
@end



@interface LDCMyCenterViewController ()
@property (nonatomic , strong)NSDictionary  *baseDic;
@property (nonatomic , strong)NSString      *QRCode_Addr;
@end

@implementation LDCMyCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNaviTitle:@"个人中心"];
    self.view.backgroundColor = [UIColor colorWithHexString:@"f2f2f2"];
    [self.view addSubview:self.displayView];
    self.displayView.backgroundColor=[UIColor clearColor];
    self.displayView.separatorStyle=UITableViewCellSeparatorStyleNone;
    // Do any additional setup after loading the view.
    [self setNaviRightImgButton:@"btn_set"];
}

- (void)clickRightButton:(id)sender {
    [self.navigationController pushViewController:[[KDSettingViewController alloc] init] animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [self requestGetUserInfo:@"base"];
}

//网络交互
//getUserInfo
-(void)requestGetUserInfo:(NSString *)type
{
    [CSHApiClient getUserInfoType:type didFinishLoaded:^(KXJson *json, NSString *path) {
        //
        if ([[json getStringForKey:@"code"] intValue] == 200) {
            NSDictionary *dic1 = [NSDictionary dictionaryWithDictionary:[json getObjectForKey:@"data"]];
            self.baseDic = dic1;
            [_displayView reloadData];
            
//            if ([_baseDic[@"is_examine"] intValue] == 1) {
                [self RequestGetQRcodeIs_examine:[NSString stringWithFormat:@"%@",_baseDic[@"is_examine"]]];
//            }
        }
    } didFailLoaded:^(NSError *error, NSString *path) {
        //
    }];
}
//getPatientJudge
- (void)requestGetPatientJudge
{
    [CSHApiClient getPatientJudgeDidFinishLoaded:^(KXJson *json, NSString *path) {
        if ([[json getStringForKey:@"code"] intValue] == 200) {
            NSArray *arr = [NSArray arrayWithArray:[json getObjectForKey:@"data"]];
            KDPatientJudgeViewController    *controller = [[KDPatientJudgeViewController alloc] init];
            controller.urlStr = arr.firstObject;
            [self.navigationController pushViewController:controller animated:YES];
        }
    } didFailLoaded:^(NSError *error, NSString *path) {
    }];
}
//getQRcode
- (void)RequestGetQRcodeIs_examine:(NSString *)Is_examine
{
    [CSHApiClient getQRcodeIs_examine:Is_examine didFinishLoaded:^(KXJson *json, NSString *path) {
        //
        if ([[json getStringForKey:@"code"] intValue] == 200) {
            NSDictionary *dic1 = [NSDictionary dictionaryWithDictionary:[json getObjectForKey:@"data"]];
            self.QRCode_Addr = dic1[@"QRCode_Addr"];
            [_displayView reloadData];
        }
    } didFailLoaded:^(NSError *error, NSString *path) {
        //
    }];
}

-(UITableView*)displayView{
    if (!_displayView) {
        _displayView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,SCREEN_HEIGHT-64-50)];
        _displayView.delegate=self;
        _displayView.dataSource=self;
    }
    return _displayView;
}
#pragma mark-UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    switch (indexPath.row) {
        case 0:
        {
            static NSString *identifier = @"CustomCenterHeadTableViewCell";
            
            CustomCenterHeadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                                              identifier];
            if (cell == nil) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomCenterHeadTableViewCell"  owner:self options:nil] lastObject];
            }
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            if (self.baseDic) {
                [cell reloadCell:self.baseDic];
            }
            
            return cell;
            
        }
            
            break;
        case 1:
        {
            static NSString *identifier = @"CustomCenterFootTableViewCell";
            
            CustomCenterFootTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                                                   identifier];
            if (cell == nil) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomCenterFootTableViewCell"  owner:self options:nil] lastObject];
            }
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.buttonClickBlock = ^(){
                [self requestGetPatientJudge];
            };
            if (!self.QRCode_Addr) {
                [cell.codeImageView setImageWithURLString:self.QRCode_Addr placeholderImage:[UIImage imageNamed:@"qrcode.png"]];
            }
            return cell;
 
        }
            
        default:
            return nil;
        break;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:
            return 100;
            break;
        case 1:
            return 354;
        default:
            return 0;
            break;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        [self.navigationController pushViewController:[[KDInforViewController alloc] init] animated:YES];
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
